import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @description：TODO
 * @Author MRyan
 * @Date 2020/11/8 15:42
 * @Version 1.0
 */
public class Customer2 {
    public static void main(String[] args) throws IOException {
        //建立连接
        Connection connection = RabbitMqUtil.getConnection("192.168.43.78", 5672, "/ems", "ems", "123");
        //获取通道对象
        final Channel channel = connection.createChannel();
        //****每一次只能消费一个消息
        channel.basicQos(1);
        //通道绑定对应队列
        channel.queueDeclare("work", true, false, false, null);
        //****参数1：队列名称，参数2：消息自动确认  消费者自动向   rabbitmq确认消息消费  false不会自动确认消息
        channel.basicConsume("work", false, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者2：+" + new String(body));
                //****参数1: 确认队列中的哪个消息 参数2：是否开启多个消息同时确认
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });
    }
}
